﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Text;

namespace SP_XHWeb.WebParts.KnowledgeCenterLink
{
    public partial class KnowledgeCenterLinkUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                divLink.InnerHtml = ShowLink();
            //}
        }

        private string ShowLink()
        {
            string str = "";
            string titleUrl=this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority + "/knowledgecenter/pages/share.aspx";
            //string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority + "/knowledgecenter";

            string siteUrl = this.Page.Request.Url.ToString().ToLower();
            string fullUrl = string.Empty;
            string fileUrl = string.Empty;
            string[] strsFile = null;
            int count = 0;
            if (siteUrl.Contains("&folderctid"))
            {
                fullUrl = Server.UrlDecode(siteUrl.Substring(0, siteUrl.IndexOf("&folderctid")));
                fileUrl = fullUrl.Substring(fullUrl.IndexOf("knowledgeshare/") + 15);
            }

            if (!string.IsNullOrEmpty(fileUrl))
            {
                strsFile = fileUrl.Split('/');
            }

            //str = fileUrl+"   "+count;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<a href={0} style='color:#5e3b06;'>{1}</a>", titleUrl, "知识共享");

            if (strsFile != null && strsFile.Length > 0)
            {
                count = strsFile.Length;
                string tempUrl = "share.aspx?RootFolder=";
                string linkUrl = "/knowledgecenter/knowledgeshare";
                for (int i = 0; i < count; i++)
                {
                    linkUrl += "/" + strsFile[i].ToString();
                    sb.Append(" > ");
                    sb.AppendFormat("<a href={0} style='color:#5e3b06;'>{1}</a>", tempUrl + Server.UrlEncode(linkUrl) + "&folderctid=", strsFile[i].ToString());
                }
            }

            str = sb.ToString();
            return str;
        }
    }
}
